﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Xml;
using RedSand.Web.Language;

namespace RedSand.Site
{

    public static class DropDownListHelper
    {
        private static XmlNode GetXmlRoot()
        {
            string code = RsLanguage.LanguageCode;
            string file = HttpContext.Current.Server.MapPath("/App_Data/DropDownListOption.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(file);
            return doc.DocumentElement;
        }
        private static XmlNodeList GetXmlNodeList(string optionDropDownListName)
        {
            string query = "DropDownList[@name='{0}']/Item";
            XmlNode root = GetXmlRoot();
            return  root.SelectNodes(string.Format(query, optionDropDownListName));
        }
        private static SelectList GetSelectList(bool all, string optionDropDownListName, object selectedValue)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            if (all) list.Add(new SelectListItem { Value = "", Text = "" });
            XmlNodeList xmlList = GetXmlNodeList(optionDropDownListName);
            foreach (XmlNode node in xmlList)
            {
                XmlAttribute valueAttr = node.Attributes["value"];
                XmlAttribute tagAttr = node.Attributes["tag"];
                XmlAttribute groupAttr = node.Attributes["tag"];
                string value = valueAttr == null ? "" : valueAttr.Value;
                string text = tagAttr == null ? "" : RsLanguage.Translate(tagAttr.Value, groupAttr == null ? "Global" : groupAttr.Value);
                list.Add(new SelectListItem { Value = value, Text = text });
            }
            return new SelectList(list, "Value", "Text", selectedValue);
        }

        #region status
        public static SelectList GetStatusList(bool all)
        {
            return GetStatusList(all, null);
        }
        public static SelectList GetStatusList(bool all, object selectValue)
        {
            return GetSelectList(all, "Status", selectValue);
        }
        public static SelectList GetStatusRequestList(bool all)
        {
            return GetStatusList(all, null);
        }
        public static SelectList GetStatusRequestList(bool all, object selectValue)
        {
            return GetSelectList(all, "RequestStatus", selectValue);
        }
        public static SelectList GetNameTypeCategoryList(bool all)
        {
            return GetNameTypeCategoryList(all, null);
        }
        public static SelectList GetNameTypeCategoryList(bool all, object selectValue)
        {
            return GetSelectList(all, "Category", selectValue);
        }
        public static MvcHtmlString GetStatusString(object status)
        {
            string value = (Convert.ToByte(status) == 0 ? "" : status.ToString());
            var result = GetStatusList(true, null).Where(item => item.Value == value);
            return MvcHtmlString.Create(result.Count() == 0 ? "" : result.First().Text);
        }
        public static MvcHtmlString GetStatusRequestString(object status)
        {
            string value = (Convert.ToByte(status) == 0 ? "" : status.ToString());
            var result = GetStatusRequestList(true, null).Where(item => item.Value == value);
            return MvcHtmlString.Create(result.Count() == 0 ? "" : result.First().Text);
        }
        public static MvcHtmlString GetNameTypeCategoryString(object Title)
        {
            string value = (Convert.ToByte(Title) == 0 ? "" : Title.ToString());
            var result = GetNameTypeCategoryList(true, null).Where(item => item.Value == value);
            return MvcHtmlString.Create(result.Count() == 0 ? "" : result.First().Text);
        }
        #endregion

        #region new type
        public static SelectList GetNewsTypeList(bool all)
        {
            return GetNewsTypeList(all, null);
        }
        public static SelectList GetNewsTypeList(bool all, object selectValue)
        {
            return GetSelectList(all, "NewsType", selectValue);
        }
        public static MvcHtmlString GetNewsTypeString(object type)
        {
            string value = (Convert.ToByte(type) == 0 ? "" : type.ToString());
            var result = GetNewsTypeList(true, null).Where(item => item.Value == value);
            return MvcHtmlString.Create(result.Count() == 0 ? "" : result.First().Text);
        }
        #endregion

        #region new hot
        public static SelectList GetNewsHotList(bool all)
        {
            return GetNewsHotList(all, null);
        }
        public static SelectList GetNewsHotList(bool all, object selectValue)
        {
            return GetSelectList(all, "Priority", selectValue);
        }
        #endregion

        #region product type
        public static SelectList GetProductTypeList(bool all)
        {
            return GetProductTypeList(all, null);
        }
        public static SelectList GetProductTypeList(bool all, object selectValue)
        {
            return GetSelectList(all, "ProductType", selectValue);
        }
        public static MvcHtmlString GetProductTypeString(object type)
        {
            string value = (Convert.ToByte(type) == 0 ? "" : type.ToString());
            var result = GetProductTypeList(true, null).Where(item => item.Value == value);
            return MvcHtmlString.Create(result.Count() == 0 ? "" : result.First().Text);
        }
        #endregion

        #region product hot
        public static SelectList GetProductHotList(bool all)
        {
            return GetProductHotList(all, null);
        }
        public static SelectList GetProductHotList(bool all, object selectValue)
        {
            return GetSelectList(all, "Priority", selectValue);
        }
        #endregion

        #region language
        public static SelectList GetLanguageList()
        {
            return GetLanguageList(null);
        }
        public static SelectList GetLanguageList(object selectValue)
        {
            List<Language> langs = RsLanguage.Languages;
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (Language lang in langs)
            {
                list.Add(new SelectListItem { Text = lang.Name, Value = lang.Code });
            }
            return new SelectList(list, "Value", "Text", selectValue);
        }
        #endregion

        #region order
        public static SelectList GetOrderList(int childCount)
        {
            return GetOrderList(childCount, null);
        }
        public static SelectList GetOrderList(int childCount, object selectValue)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            for (int i = 1; i <= childCount; i++)
            {
                list.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
            }
            return new SelectList(list, "Value", "Text", selectValue);
        }
        #endregion
    }
}
